package org.nViron.nvPlaces;

//~--- non-JDK imports --------------------------------------------------------

import java.util.HashMap;

import org.nViron.nvData.nvDataEntity;
import org.nViron.nvOrganism.nvOrganismMgr;
import org.nViron.nvOrganism.nvStateMgr;
import org.nViron.nvSim.nvSimulator;

/**
 * Management class that manages places
 * @author Roland Haring <p>
 * <script type="text/javascript" src="http://www.ohloh.net/p/376434/widgets/project_basic_stats.js"></script>
 */
public class nvPlacesMgr extends nvOrganismMgr {
    private final HashMap enitityLookupTable = new HashMap();
    private final nvClusterer clusterer = new nvBasicClusterer();

    public nvPlacesMgr(nvSimulator simulator) {
        super(simulator);
    }

    public void addEntitiyToPlace(nvDataEntity entity, nvPlace place) {
        place.addEntity(entity, clusterer);
        enitityLookupTable.put(entity.getId(), place);
    }

    public nvPlace findPlaceForEntity(nvDataEntity entity) {
        return clusterer.findPlaceForEntity(getOrganisms(), entity);
    }

    public nvClusterer getClusterer() {
        return clusterer;
    }

    public HashMap getPlaces() {
        return getOrganisms();
    }

    public nvPlace lookupPlaceByEntityId(nvDataEntity entity) {
        return (nvPlace) enitityLookupTable.get(entity.getId());
    }

    public void removePlace(nvPlace place) {
        place.setState(nvStateMgr.DEGENERATING);
    }

    @Override
    public void update(float timeStep) {

        // update places
        super.update(timeStep);
        clusterer.balancePlaces(getOrganisms());
    }
}



